package com.city.parking.map;

import java.util.ArrayList;

import com.city.parking.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

public class RemoteOfflineMapListAdapter extends
        ArrayAdapter<OfflineMapRemoteItemModel> {

	private final Context context;
	private final ArrayList<OfflineMapRemoteItemModel> remoteListInfo;

	public RemoteOfflineMapListAdapter(Context context,
	        ArrayList<OfflineMapRemoteItemModel> remoteListInfo) {

		super(context, R.layout.offline_map_item, remoteListInfo);

		this.context = context;
		this.remoteListInfo = remoteListInfo;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		LayoutInflater inflater = (LayoutInflater) context
		        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		View rowView = null;

		if ((remoteListInfo.get(position).getItemType() == 0)
		        || (remoteListInfo.get(position).getItemType() == 2)) {
			if (remoteListInfo.get(position).isInSubList()) {
				rowView = inflater.inflate(R.layout.offline_map_subitem,
				        parent, false);
				TextView titleView = (TextView) rowView
				        .findViewById(R.id.subitem_header);
				titleView.setText(remoteListInfo.get(position).getCityName());

				TextView downloadStatusView = (TextView) rowView
				        .findViewById(R.id.subitem_download_status);
				Button downloadIcon = (Button) rowView
				        .findViewById(R.id.subitem_btn_downloadable);

				if (remoteListInfo.get(position).getDownloadStatus() != 0) {
					downloadStatusView.setText("("
					        + context.getResources().getString(
					                R.string.som_btn_downloaded) + ")");
					downloadIcon
					        .setBackgroundResource(R.drawable.ic_downloadable_false);
				} else {
					downloadStatusView.setText("");
					downloadIcon
					        .setBackgroundResource(R.drawable.ic_downloadable_true);
				}

				TextView packSizeView = (TextView) rowView
				        .findViewById(R.id.subitem_map_pack_size);
				packSizeView
				        .setText(remoteListInfo.get(position).getPackSize());
			} else {
				rowView = inflater.inflate(R.layout.offline_map_item, parent,
				        false);
				TextView titleView = (TextView) rowView
				        .findViewById(R.id.header);
				titleView.setText(remoteListInfo.get(position).getCityName());

				TextView downloadStatusView = (TextView) rowView
				        .findViewById(R.id.download_status);
				Button downloadIcon = (Button) rowView
				        .findViewById(R.id.btn_downloadable);

				if (remoteListInfo.get(position).getDownloadStatus() != 0) {
					downloadStatusView.setText("("
					        + context.getResources().getString(
					                R.string.som_btn_downloaded) + ")");
					downloadIcon
					        .setBackgroundResource(R.drawable.ic_downloadable_false);
				} else {
					downloadStatusView.setText("");
					downloadIcon
					        .setBackgroundResource(R.drawable.ic_downloadable_true);
				}

				TextView packSizeView = (TextView) rowView
				        .findViewById(R.id.map_pack_size);
				packSizeView
				        .setText(remoteListInfo.get(position).getPackSize());
			}

		} else if (remoteListInfo.get(position).getItemType() == 1) {
			rowView = inflater
			        .inflate(R.layout.offline_map_item, parent, false);
			TextView titleView = (TextView) rowView.findViewById(R.id.header);
			titleView.setText(remoteListInfo.get(position).getCityName());

			TextView downloadStatusView = (TextView) rowView
			        .findViewById(R.id.download_status);
			Button downloadIcon = (Button) rowView
			        .findViewById(R.id.btn_downloadable);
			Button expandIcon = (Button) rowView.findViewById(R.id.btn_expand);
			TextView packSizeView = (TextView) rowView
			        .findViewById(R.id.map_pack_size);

			downloadStatusView.setVisibility(View.GONE);
			downloadIcon.setVisibility(View.GONE);
			packSizeView.setVisibility(View.GONE);
			expandIcon.setVisibility(View.VISIBLE);
		} else { // group header
			rowView = inflater.inflate(R.layout.offline_map_group_header_item,
			        parent, false);
			TextView titleView = (TextView) rowView
			        .findViewById(R.id.group_header);
			titleView.setText(remoteListInfo.get(position).getCityName());
		}

		return rowView;
	}
}